package com.xdf.whiteaccount.service.impl;

import com.xdf.whiteaccount.dao.JrkbillsumMapper;
import com.xdf.whiteaccount.entity.Jrkbillsum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdf.whiteaccount.entity.GJrkbill;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

import com.xdf.whiteaccount.dao.GJrkbillMapper;
import com.xdf.whiteaccount.service.GJrkbillService;

/**
 * @Description : com.xdf.whiteaccount.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : 张柯
 * @Date : 2021-05-22 08:16:02
 */
@Service
public class GJrkbillServiceImpl implements GJrkbillService {
    @Autowired
    private GJrkbillMapper dao;
    @Autowired
    private JrkbillsumMapper jrkbillsumMapper;

    /**
     * @Describe 新增方法
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(GJrkbill record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(GJrkbill record) throws Exception {
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<GJrkbill> list) throws Exception {
        return dao.multiInsert(list);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(GJrkbill record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(GJrkbill record) throws Exception {
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(GJrkbill record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<GJrkbill> list) throws Exception {
        return dao.multiUpdate(list);
    }

    /**
     * @Describe 根据主键查询
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    public GJrkbill selectByPrimaryKey(Integer id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    public List<GJrkbill> listQuery(GJrkbill record) throws Exception {
        return dao.selectByParam(record);
    }

    /**
     * @Describe 根据主键删除
     * @author 张柯
     * @Date 2021-05-22 08:16:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Jrkbillsum delete(Integer id) {
        //为了不让删除Jrkbillsum后接口报错，创建特殊的Jrkbillsum作为返回对象
        Jrkbillsum j = new Jrkbillsum();
        j.setId(0);

        //判断是否为非法id
        if (id <= 0) return null;
        //通过id查询GJrkbill对象
        GJrkbill gJrkbill = dao.selectByPrimaryKey(id);

        //判断是否查询出GJrkbill对象
        if (gJrkbill == null) return null;
        String bBarcode = gJrkbill.getBBarcode();

        //判断货架号是否为空
        if (bBarcode == null) return null;
        //通过货架号查询Jrkbillsum对象
        Jrkbillsum jrkbillsum = jrkbillsumMapper.selectByBBarcode(bBarcode);
        //BigDecimal精确计算
        BigDecimal newQty = jrkbillsum.getQty().subtract(gJrkbill.getBGj());
        jrkbillsum.setQty(newQty);
        jrkbillsum.setPs(jrkbillsum.getPs() - 1);
        //修改数据
        jrkbillsumMapper.updateByPrimaryKeySelective(jrkbillsum);

        //删除GJrkbill对象
        dao.deleteByPrimaryKey(id);
        //若匹数为0，则删除对应的Jrkbillsum对象
        if (jrkbillsum.getPs() <= 0) {
            jrkbillsumMapper.deleteByPrimaryKey(jrkbillsum.getId());
            return j;
        }

        return jrkbillsumMapper.selectByBBarcode(gJrkbill.getBBarcode());
    }
}